/**
 * 
 */
package net.e_agri.wap;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.e_agri.cache.AgriCacheManager;
import net.e_agri.cache.ContentCacheManager;
import net.htmlparser.jericho.Source;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author rota
 *
 */
@SuppressWarnings("serial")
public class CacheServlet extends HttpServlet {

	private static final Log log = LogFactory.getLog(CacheServlet.class);

	private static ContentCacheManager cacheManager = null;

	public void init(){

		cacheManager = new AgriCacheManager();
	}

	@SuppressWarnings("unchecked")
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
	  		throws ServletException, IOException
	{
		StringBuffer logBuf = new StringBuffer();
		for(String paramName : (Set<String>)req.getParameterMap().keySet())
			logBuf.append("\n\t").append(paramName).append(" = [")
				.append(req.getParameter(paramName)).append("]");

		log.info(logBuf.toString());

		if(req.getParameter("urls") == null)
			return;
		if(req.getParameter("urls").trim().length() == 0)
			return;

		List<String> urlsAll = Arrays.asList(req.getParameter("urls").split(","));
		List<String> urls = new ArrayList<String>();

		int processedIndex = 0;
		final int BATCH_SIZE = 5;
		long costBase = 0;
		Map<String, String> topicsMap = null;

		while(processedIndex < urlsAll.size()){
			urls.clear();
			for(int i = 0; i<BATCH_SIZE; i++) {
				urls.add(urlsAll.get(processedIndex));
				processedIndex++;
				if(processedIndex == urlsAll.size())
					break;
			}

			costBase = System.currentTimeMillis();
			topicsMap = HttpMethod.doGetAsync(urls);
			for(String url : topicsMap.keySet()) {
				topicsMap.put(url, AgriCacheManager.assignConverter(url)
						.parseSource(new Source(topicsMap.get(url)), url));
			}
	
			cacheManager.putCacheAll(topicsMap, System.currentTimeMillis() - costBase);
		}
	}
}
